package com.sk.weichat.ui.me.wallet;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.security.rp.RPSDK;
import com.sk.weichat.R;
import com.sk.weichat.bean.IsBindAlipayBean;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.pay.sk.AlipayAuth;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.me.wallet.view.AuthCallbackView;
import com.sk.weichat.ui.me.wallet.view.RealNameAuthenticationView;
import com.sk.weichat.ui.me.wallet.view.SetpswAuthenticationView;
import com.sk.weichat.ui.me.wallet.viewmode.RealNameAuthenticationViewMode;
import com.sk.weichat.ui.me.wallet.viewmode.SetpswAuthenticationViewMode;
import com.sk.weichat.util.ToastUtil;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
* 设置密码身份验证
* */
public class SetpswAuthenticationActivity extends BaseActivity implements View.OnClickListener , AuthCallbackView, SetpswAuthenticationView, RealNameAuthenticationView {


    private ImageView ivBack;
    private TextView tv_title_center;
    private LinearLayout ll_alipay_bind;
    private TextView tv_alipay_nickname;
    private TextView tv_alipay_hint;
    private Button btn_bind;
    private TextView tv_no_bind_hint;

    private SetpswAuthenticationViewMode mSetpswAuthenticationViewMode;
    private String aliPayNickName;
    private boolean isBind = true;//绑定支付宝

    private int RealNameAuthStatus;
    private RealNameAuthenticationViewMode mRealNameAuthenticationViewMode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_psw_authentication);
        getSupportActionBar().hide();

        RealNameAuthStatus = getIntent().getIntExtra("RealNameAuthStatus",0);
        initView();
    }
    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(String str) {
        //绑定或解绑成功  重新获取钱包数据刷新
        if (str.equals("set_question")) {
            finish();
        }
    }
    private void initView(){
        ivBack = findViewById(R.id.iv_title_left);
        tv_title_center = findViewById(R.id.tv_title_center);
        tv_title_center.setText(getString(R.string.identity_auth));
        ll_alipay_bind = findViewById(R.id.ll_alipay_bind);
        tv_alipay_nickname = findViewById(R.id.tv_alipay_nickname);
        tv_alipay_hint = findViewById(R.id.tv_alipay_hint);
        btn_bind = findViewById(R.id.btn_bind);
        tv_no_bind_hint = findViewById(R.id.tv_no_bind_hint);
        tv_no_bind_hint.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
        tv_no_bind_hint.getPaint().setAntiAlias(true);//抗锯齿
        ivBack.setOnClickListener(this);
        btn_bind.setOnClickListener(this);
        tv_no_bind_hint.setOnClickListener(this);

        mSetpswAuthenticationViewMode = new SetpswAuthenticationViewMode(coreManager,this);
        mRealNameAuthenticationViewMode = new RealNameAuthenticationViewMode(coreManager,this);
        mSetpswAuthenticationViewMode.isBindAlipay();
    }

    @Override
    public void onClick(View v) {
        if (v==ivBack){
            finish();
        }else if (v==btn_bind){
            if (aliPayNickName.equals("false")){//没绑定  授权
                isBind = true;
                mSetpswAuthenticationViewMode.getAlipayAutoMsg();
            }else {//身份验证
                isBind = false;
                mSetpswAuthenticationViewMode.getAlipayAutoMsg();
            }
        }else if (v==tv_no_bind_hint){
            if (RealNameAuthStatus==1){//已认证
                startActivity(new Intent(this, RealNameInfoActivity.class));
            }else {//未实名认证
                mRealNameAuthenticationViewMode.getAliyunToken();
            }
        }
    }

    /**
    * 判断是否绑定支付宝
    * */
    @Override
    public void isBingAlipayResult(ObjectResult<IsBindAlipayBean> result) {
        if (result.getResultCode()==1){
            if (result.getData().getState().equals("BINDING")){
                aliPayNickName = result.getData().getAliName();
                ll_alipay_bind.setVisibility(View.VISIBLE);
                tv_alipay_nickname.setText(aliPayNickName);
                tv_alipay_hint.setText(getString(R.string.again_bind_hint));
                tv_no_bind_hint.setVisibility(View.GONE);
                btn_bind.setText(getString(R.string.title_sms_code_for_identify));
            }else {//没绑定支付宝
                ll_alipay_bind.setVisibility(View.GONE);
                tv_alipay_hint.setText(getResources().getText(R.string.unbind_alipay_hint));
                tv_no_bind_hint.setVisibility(View.VISIBLE);
                btn_bind.setText(getString(R.string.alipay_bind));
            }
        }else {
            ToastUtil.showToast(this,result.getResultMsg());
        }
    }

    /**
    * 支付宝授权信息返回
    * */
    @Override
    public void AlipayAuthMsgResult(ObjectResult<String> result) {
        if (result.getResultCode()==1){
            AlipayAuth alipayAuth = new AlipayAuth(this);
            alipayAuth.callAuth(this,result.getData(),isBind);
        }else {
            ToastUtil.showToast(this,result.getResultMsg());
        }
    }
    /**
     * 支付宝授权成功返回
     */
    @Override
    public void AlipayAuthCallbackResult(String authCode) {
        if (TextUtils.isEmpty(authCode)){
            showLoadFailMsg(getString(R.string.alipay_auth_cancle));
        }else {
            if (isBind){
                mSetpswAuthenticationViewMode.setAlipayAutoCode(authCode);
            }else {
                mSetpswAuthenticationViewMode.compareAlipay(authCode);
            }
        }
    }

    /**
    *  支付宝绑定成功
    * */
    @Override
    public void AlipayAutoCodeResult(ObjectResult<String> result) {
        if (result.getResultCode()==1){
            EventBus.getDefault().post("bind_success");
            aliPayNickName = result.getData();
            ll_alipay_bind.setVisibility(View.VISIBLE);
            tv_alipay_nickname.setText(aliPayNickName);
            tv_alipay_hint.setText(getResources().getText(R.string.bind_alipay_hint));
            tv_no_bind_hint.setVisibility(View.GONE);
            btn_bind.setText(getString(R.string.unbind));
            showLoadFailMsg(getString(R.string.bind_success));
            //绑定成功直接跳转支付密码页面
            startActivity(new Intent(this,SetPayPwdActivity.class).putExtra("PayPasswordType","ALIPAY_AUTH"));
            finish();
        }else {
            showLoadFailMsg(result.getResultMsg());
        }
    }

    /**
    *  验证支付宝账号是否一致  接口返回
    * */
    @Override
    public void CompareAlipayResult(ObjectResult<String> result) {
        if (result.getResultCode()==1){
            if (result.getData().equals("true")){//
                startActivity(new Intent(this,SetPayPwdActivity.class).putExtra("PayPasswordType","ALIPAY_AUTH"));
                finish();
            }else {
                showLoadFailMsg("支付宝账号验证不通过");
            }
        }else {
            showLoadFailMsg(result.getResultMsg());
        }
    }



    @Override
    public void getAliyunTokenResult(ObjectResult<String> reslut) {
        if (reslut.getResultCode() == 1) {
            RPSDK.start(reslut.getData(), SetpswAuthenticationActivity.this,
                    new RPSDK.RPCompletedListener() {
                        @Override
                        public void onAuditResult(RPSDK.AUDIT audit, String code) {
                            if (audit == RPSDK.AUDIT.AUDIT_PASS) { //认证通过
                                mRealNameAuthenticationViewMode.AiyunAuthBack("1");
                            } else if (audit == RPSDK.AUDIT.AUDIT_FAIL) { //认证不通过
                                ToastUtil.showToast(SetpswAuthenticationActivity.this, R.string.auth_fail);
                            } else if (audit == RPSDK.AUDIT.AUDIT_NOT) { //未认证，具体原因可通过code来区分（code取值参见下方表格），通常是用户主动退出或者姓名身份证号实名校验不匹配等原因，导致未完成认证流程
                                ToastUtil.showToast(SetpswAuthenticationActivity.this, R.string.auth_cancle);
                            }
                        }
                    });
        } else {
            ToastUtil.showToast(this, reslut.getResultMsg());
        }
    }

    @Override
    public void AiyunAuthBackResult(ObjectResult<String> result) {
        if (result.getResultCode() == 1) {
            RealNameAuthStatus = 1;//更改实名认证 状态
            EventBus.getDefault().post("auth_success");
            showLoadFailMsg(getString(R.string.auth_success));
            startActivity(new Intent(mContext, SetIdentifyQuestionActivity.class));
        } else {
            showLoadFailMsg(result.getResultMsg());
        }
    }
    @Override
    public void showProgress() {
        if (!DialogHelper.isShowing()) {
            DialogHelper.showDefaulteMessageProgressDialog(this);
        }
    }

    @Override
    public void hideProgress() {
        DialogHelper.dismissProgressDialog();
    }

    @Override
    public void showLoadFailMsg(String err) {
        ToastUtil.showToast(this, err);
    }



}
